package com.example.lib_net.retrofit.calladapter;

import androidx.lifecycle.LiveData;

import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import retrofit2.Call;
import retrofit2.CallAdapter;
import retrofit2.Retrofit;

public class LiveDataCallFactory extends CallAdapter.Factory {

    public static LiveDataCallFactory create(){
        return new LiveDataCallFactory();
    }

    @Override
    public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {

        if (!(returnType instanceof ParameterizedType)){
            throw new IllegalArgumentException("返回值必须是可参数化的类型");
        }

        Class<?> rawType = CallAdapter.Factory
                .getRawType(returnType);

        if (rawType != LiveData.class && rawType != Call.class){
            throw new IllegalArgumentException("返回值必须是LiveData|Call的类型");
        }

        //泛型类型
        Type t = CallAdapter.Factory
                .getParameterUpperBound(0, (ParameterizedType) returnType);

        if (rawType == Call.class){
            return new DefaultCallAdapter<>(t);
        }

        return new LiveDataCallAadapter<>(t);
    }
}